//
//  ThreadTest.m
//  多线程相关
//
//  Created by song on 15/8/8.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ThreadTest.h"

@implementation ThreadTest

-(void)runtest
{
    for(int i=0 ;i<10 ;i++)
    {
        NSLog(@"%@ ----- %d",[NSThread currentThread],i);
    }
}

-(void)testThread01
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread.name=@"001";
    [thread start];
}

-(void)TestThread02
{
    [NSThread detachNewThreadSelector:@selector(saleTicket) toTarget:self withObject:nil];

}

-(void)TestThread03
{
    [self performSelectorInBackground:@selector(saleTicket) withObject:nil];
}

-(void)saleThread
{
    self.leftCount = 200;
    NSThread *thread0 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread0.name=@"000";
    [thread0 start];
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread1.name=@"001";
    [thread1 start];
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
    thread2.name=@"002";
    [thread2 start];
}


-(void) saleTicket
{
    while(1)
    {
        int count = self.leftCount;
        if(count >0)
        {
            [NSThread sleepForTimeInterval:0.0002];
            self.leftCount = count -1;
            NSThread *thread = [NSThread currentThread];
            NSLog(@"%@ 卖出去了一张 ，剩余 %d",thread , self.leftCount);
        }else{
            [NSThread exit];
        }
    }
}


-(void)NSQueue001
{
//    需要start 才可以启动线程
//    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(down) object:nil];
//    [operation start];
   // 通过类方法
//    NSBlockOperation *operation  = [NSBlockOperation blockOperationWithBlock:^{
//        
//    }];
    //只要NSBlockOperation 封装的操作数》1 就会异步执行操作
    NSBlockOperation *operation  =  [[NSBlockOperation alloc] init];
    
    [operation addExecutionBlock:^{
        [self down];
    }];
    
    
}

-(void)NSQueue002
{
    
    NSInvocationOperation *operation01 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
    [operation01 setQueuePriority:NSOperationQueuePriorityHigh]; // 设置operation 的优先级
    NSInvocationOperation *operation02 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
    
    NSBlockOperation *operation03 = [[NSBlockOperation alloc]init];
    
    [operation03 addExecutionBlock:^{
        for(int i =0;i< 10;i++)
        {
            NSLog(@"-----run --------%@ ",[NSThread currentThread]);
        }
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 2;  //设置最多执行的线程数目
    [operation02 addDependency:operation03];
    [operation03 addDependency:operation01];
    
    [queue addOperation:operation01];
    [queue addOperation:operation02];
    [queue addOperation:operation03];
}

-(void)down
{
    NSLog(@"-------download ------ %@" ,[NSThread currentThread]);
}
-(void)download
{
    for(int i =0;i< 10;i++)
    {
        NSLog(@"-----run --------%@ ",[NSThread currentThread]);
    }
}


-(void)gcd001
{
    // 穿行队列 运行异步任务
    dispatch_queue_t  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        NSLog(@"----下载图片1  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(queue, ^{
         NSLog(@"----下载图片2  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(queue, ^{
         NSLog(@"----下载图片3  ------%@", [NSThread  currentThread]);
    });
}

-(void)gcd002
{
    dispatch_queue_t  squeue = dispatch_queue_create("testserial", DISPATCH_QUEUE_SERIAL);
    
    
    dispatch_async(squeue, ^{
        NSLog(@"----serial  async  下载图片1  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(squeue, ^{
        NSLog(@"----serial async 下载图片2  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(squeue, ^{
        NSLog(@"----serial async 下载图片3  ------%@", [NSThread  currentThread]);
    });
    
    
    
    dispatch_queue_t  cqueue = dispatch_queue_create("testcurrent", DISPATCH_QUEUE_CONCURRENT);
    
    
    dispatch_async(cqueue, ^{
        NSLog(@"----current  async  下载图片1  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(cqueue, ^{
        NSLog(@"----current 下载图片2  ------%@", [NSThread  currentThread]);
    });
    
    dispatch_async(cqueue, ^{
        NSLog(@"----current 下载图片3  ------%@", [NSThread  currentThread]);
    });
    
}


@end
